package com.douxg.fastCoding.web.controller;

import cn.hutool.core.util.StrUtil;
import com.douxg.fastCoding.spring.util.CheckUtil;
import com.douxg.fastCoding.spring.util.DownUtil;
import com.douxg.fastCoding.spring.vo.Result;
import com.douxg.fastCoding.web.consts.FunctionEnum;
import com.douxg.fastCoding.web.dto.CodeGenerateDto;
import com.douxg.fastCoding.web.dto.EnjoyDto;
import com.douxg.fastCoding.web.service.CodeService;
import com.douxg.fastCoding.web.service.EnjoyService;
import com.douxg.fastCoding.web.vo.ElSelectVo;
import com.douxg.fastCoding.web.vo.ElTabVo;
import com.douxg.fastCoding.web.vo.EnjoyVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 代码生成
 * @Auther douxg
 * @Date 2020/6/15 20:29
 */
@RequestMapping("fc/code")
@RestController
public class CodeController {
    @Resource
    private CodeService service;
    @Resource
    private EnjoyService enjoyService;

    @PostMapping("generate")
    public Result generate(@RequestBody CodeGenerateDto dto) {
        String code = service.generate(dto);
        return Result.success(code);
    }

    @PostMapping("groups")
    public Result groups() {
        EnjoyDto enjoyDto = new EnjoyDto();
        enjoyDto.setFunctionName(FunctionEnum.CODE.getName());
        List<String> groups = enjoyService.groups(enjoyDto);
        List<ElSelectVo> list = ElSelectVo.createListByValue(groups);
        return Result.success(list);
    }

    @PostMapping("names")
    public Result names(String groupName) {
        CheckUtil.isBlank(groupName);
        EnjoyDto enjoyDto = new EnjoyDto();
        enjoyDto.setGroupName(groupName);
        enjoyDto.setFunctionName(FunctionEnum.CODE.getName());
        List<EnjoyVo> enjoyVos = enjoyService.list(enjoyDto);
        List<ElTabVo> elTabVos = enjoyVos.stream().map(enjoyVo -> ElTabVo.createByValue(enjoyVo.getName())).collect(Collectors.toList());
        return Result.success(elTabVos);
    }

    @GetMapping("down")
    public Result down(HttpServletResponse response, String dbAlias, String tableName, String groupName, String enjoyName) {
        String content;
        CodeGenerateDto codeGenerateDto = new CodeGenerateDto();
        codeGenerateDto.setDbAlias(dbAlias);
        codeGenerateDto.setTableName(tableName);
        codeGenerateDto.setGroupName(groupName);
        String javaTableName = StrUtil.toCamelCase(tableName);
        if (StrUtil.isBlank(enjoyName)) {
            CheckUtil.isBlank(groupName);
            EnjoyDto enjoyDto = new EnjoyDto();
            enjoyDto.setFunctionName(FunctionEnum.CODE.getName());
            enjoyDto.setGroupName(groupName);
            List<EnjoyVo> enjoyVos = enjoyService.list(enjoyDto);
            List<DownUtil.DownFile> list = new ArrayList<>();
            for (EnjoyVo enjoyVo : enjoyVos) {
                enjoyName = enjoyVo.getName();
                codeGenerateDto.setEnjoyName(enjoyName);
                content = service.generate(codeGenerateDto);
                DownUtil.DownFile downFile = new DownUtil.DownFile();
                downFile.setContent(content);
                downFile.setName(javaTableName + StrUtil.upperFirst(enjoyName) + "." + enjoyVo.getFileType());
                list.add(downFile);
            }
            DownUtil.downZip(tableName + ".zip", list, response);
        } else {
            EnjoyDto enjoyDto = new EnjoyDto();
            enjoyDto.setFunctionName(FunctionEnum.CODE.getName());
            enjoyDto.setGroupName(groupName);
            enjoyDto.setName(enjoyName);
            EnjoyVo enjoyVo = enjoyService.selectOne(enjoyDto);
            codeGenerateDto.setEnjoyName(enjoyName);
            content = service.generate(codeGenerateDto);
            DownUtil.down(javaTableName + StrUtil.upperFirst(enjoyName) + "." + enjoyVo.getFileType(), content, response);
        }
        return Result.success();
    }

    private String enjoyDownName(String enjoyName) {
        if (enjoyName.toLowerCase().endsWith("xml")) {
            return enjoyName + ".xml";
        } else {
            return enjoyName + ".java";
        }
    }

}
